import pandas as pd
import numpy as np
import sklearn

#********* Begin *********#

train_data = pd.read_csv('./train.csv')
test_data = pd.read_csv('./test.csv')


train_data['Initial']=0
for i in train_data:
    train_data.loc[:, 'Initial'] = train_data.Name.str.extract('([A-Za-z]+)\.',expand=False) #lets extract the Salutations


train_data.loc[:, 'Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Master'],['Miss','Miss','Miss','Other','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Other'],inplace=True)

train_data.groupby('Initial')['Age'].mean()

train_data.loc[(train_data.Age.isnull())&(train_data.Initial=='Mr'),'Age']=33
train_data.loc[(train_data.Age.isnull())&(train_data.Initial=='Mrs'),'Age']=36
train_data.loc[(train_data.Age.isnull())&(train_data.Initial=='Miss'),'Age']=22
train_data.loc[(train_data.Age.isnull())&(train_data.Initial=='Other'),'Age']=46



train_data['Embarked'].fillna('S', inplace=True)

train_data['Age_band']=0
train_data.loc[train_data['Age']<=16,'Age_band']=0
train_data.loc[(train_data['Age']>16)&(train_data['Age']<=32),'Age_band']=1
train_data.loc[(train_data['Age']>32)&(train_data['Age']<=48),'Age_band']=2
train_data.loc[(train_data['Age']>48)&(train_data['Age']<=64),'Age_band']=3
train_data.loc[train_data['Age']>64,'Age_band']=4

train_data['Family_Size']=0
train_data['Family_Size']=train_data['Parch']+train_data['SibSp']+1
train_data['Alone']=0
train_data.loc[train_data.Family_Size==1,'Alone']=1


train_data.loc[:, 'Name_Len'] = train_data['Name'].apply(lambda x : len(x))

def fff(x):
    if x < 25:
        return 0
    elif x < 35:
        return 1
    elif x < 50:
        return 2
    else:
        return 3
train_data.loc[:, 'Name_Len'] = train_data['Name_Len'].apply(fff)

train_data['Fare_cat']=0
train_data.loc[train_data['Fare']<=7.91,'Fare_cat']=0
train_data.loc[(train_data['Fare']>7.91)&(train_data['Fare']<=14.454),'Fare_cat']=1
train_data.loc[(train_data['Fare']>14.454)&(train_data['Fare']<=31),'Fare_cat']=2
train_data.loc[(train_data['Fare']>31)&(train_data['Fare']<=513),'Fare_cat']=3

train_data['Sex'].replace(['male','female'],[0,1],inplace=True)
train_data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
train_data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True)
train_data['Cabin'].replace(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'T'], [0, 1, 2, 3, 4, 5, 6, 7], inplace=True)

train_data.drop(['Name','Age','Ticket','Fare','PassengerId','Cabin'],axis=1,inplace=True)




test_data['Initial']=0
for i in test_data:
    test_data.loc[:, 'Initial'] = test_data.Name.str.extract('([A-Za-z]+)\.',expand=False) #lets extract the Salutations


test_data.loc[:, 'Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Master'],['Miss','Miss','Miss','Other','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Other'],inplace=True)

test_data.groupby('Initial')['Age'].mean()

test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Mr'),'Age']=33
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Mrs'),'Age']=36
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Miss'),'Age']=22
test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Other'),'Age']=46


test_data['Embarked'].fillna('S', inplace=True)

test_data['Age_band']=0
test_data.loc[test_data['Age']<=16,'Age_band']=0
test_data.loc[(test_data['Age']>16)&(test_data['Age']<=32),'Age_band']=1
test_data.loc[(test_data['Age']>32)&(test_data['Age']<=48),'Age_band']=2
test_data.loc[(test_data['Age']>48)&(test_data['Age']<=64),'Age_band']=3
test_data.loc[test_data['Age']>64,'Age_band']=4

test_data['Family_Size']=0
test_data['Family_Size']=test_data['Parch']+test_data['SibSp']+1
test_data['Alone']=0
test_data.loc[test_data.Family_Size==1,'Alone']=1


test_data.loc[:, 'Name_Len'] = test_data['Name'].apply(lambda x : len(x))

def fff(x):
    if x < 25:
        return 0
    elif x < 35:
        return 1
    elif x < 50:
        return 2
    else:
        return 3
test_data.loc[:, 'Name_Len'] = test_data['Name_Len'].apply(fff)

test_data['Fare_cat']=0
test_data.loc[test_data['Fare']<=7.91,'Fare_cat']=0
test_data.loc[(test_data['Fare']>7.91)&(test_data['Fare']<=14.454),'Fare_cat']=1
test_data.loc[(test_data['Fare']>14.454)&(test_data['Fare']<=31),'Fare_cat']=2
test_data.loc[(test_data['Fare']>31)&(test_data['Fare']<=513),'Fare_cat']=3

test_data['Sex'].replace(['male','female'],[0,1],inplace=True)
test_data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True)
test_data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True)
test_data['Cabin'].replace(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'T'], [0, 1, 2, 3, 4, 5, 6, 7], inplace=True)

test_data.drop(['Name','Age','Ticket','Fare','PassengerId','Cabin'],axis=1,inplace=True)

from sklearn.ensemble import RandomForestClassifier

Y = train_data['Survived']
X = train_data.drop(['Survived'], axis=1)

clf = RandomForestClassifier(n_estimators=10)
clf.fit(X, Y)
result = clf.predict(test_data)

result = pd.DataFrame({'Survived':result})
result.to_csv('./predict.csv', index=False)

#********* End *********#
